<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace Topic\Controller;
use Common\Controller\HomebaseController;
class TopicController extends HomebaseController {
	
	protected $topic_model;
	function _initialize(){
		parent::_initialize();
		$this->topic_model=D("Topic");
	}
	
	public function index() {
		$id=I("get.id");
		
		$topic_cat_model=M("TopicCat");
		
		$join = C('DB_PREFIX').'topic as b on a.id =b.cid';
		$join2 = C('DB_PREFIX').'users as c on c.id =b.uid';
		$topic=$topic_cat_model->field("a.cat_name,b.*,c.user_login,c.user_nicename")->alias("a")->join($join)->join($join2)
		->order("b.createtime desc")
		->where(array("b.id"=>$id,"b.status"=>1))
		->find();
		
		if(empty($topic)){
    	    header('HTTP/1.1 404 Not Found');
    	    header('Status:404 Not Found');
    	    
    	    return ;
    	}
		
		$data['hits'] = array('exp','hits+1');
		$this->topic_model->where(array("id"=>$id))->save($data);
		$this->assign($topic);
		$this->display(":detail");
	}
	
	public function add(){
		$this->check_login();
		$topic_cat_model=M("TopicCat");
		$topic_cats=$topic_cat_model->where(array("status"=>1))->select();
		$this->assign("topic_cats",$topic_cats);
		
		$this->display(":add");
	}
	
	/**
	 * 提交话题
	 */
	public function add_post(){
		if(IS_POST){
			$this->check_login();
			$_POST=I("post.");
			$_POST['content']=h(htmlspecialchars_decode($_POST['content']));
			$_POST['uid']=get_current_userid();
			if ($this->topic_model->field("cid,uid,title,content")->create($_POST)){
				if ($this->topic_model->add()!==false) {
					$this->success("话题发表成功！", U("topic/index/index"));
				} else {
					$this->error("话题发表失败！");
				}
			} else {
				$this->error($this->topic_model->getError());
			}
		}
	
	}
	
	public function edit(){
		$this->check_login();
		$id=I("get.id");
		$topic_cat_model=M("TopicCat");
		$topic_cats=$topic_cat_model->where(array("status"=>1))->select();
		$this->assign("topic_cats",$topic_cats);
		$topic=$this->topic_model->where(array("id"=>$id))->find();
		$this->assign($topic);
		
		$this->display(":edit");
	}
	
	
	public function edit_post(){
		if(IS_POST){
			$this->check_login();
			$uid=get_current_userid();
			$_POST=I("post.");
			$find_topic=$this->topic_model->where(array("id"=>intval($_POST['id']),"uid"=>$uid))->find();
			
			if(empty($find_topic)){
				$this->error("话题不存在！");
			}
			
			$_POST['content']=h(htmlspecialchars_decode($_POST['content']));
			if ($this->topic_model->field("id,cid,title,content")->create($_POST)){
				if ($this->topic_model->save()!==false) {
					$this->success("话题发表成功！", U("topic/index/index"));
				} else {
					$this->error("话题发表失败！");
				}
			} else {
				$this->error($this->topic_model->getError());
			}
		}
	
	}
    
    
}

